<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>echarts地图</title>
    <script src="../js/data.js"></script>
    <script src="../js/vue2.js"></script>
    <script src="../js/echarts.min.js"></script>
    <script src="../js/jquery.min.js"></script>
</head>

<body>
<div class="viewport" id="home">
    <div class="echarts" id="echarts"></div>
</div>
</body>
<script>
    var geoCoordMap = {}
    var customerBatteryCityData = []

    function GetRandomNum(Min, Max) {
        var Range = Max - Min;
        var Rand = Math.random();
        return (Min + Math.round(Rand * Range));
    }

    // 获取数据
    function getData() {
        $.get('../js/data.json', function (data) {
            getEcharts(data)
        });
    }

    // 渲染echarts
    function getEcharts(data) {
        var name = ''
        var chartDom = document.getElementById('echarts');
        var myChart = echarts.init(chartDom);
        var nameMap = '地图数据';
        echarts.registerMap(nameMap, data);
        var mapFeatures = echarts.getMap(nameMap).geoJson.features;
        mapFeatures.forEach(function (v, index) {
            customerBatteryCityData.push({
                name: v.properties.name,
                value: GetRandomNum(20, 100)
            });
            geoCoordMap[v.properties.name] = v.properties.center;
        });
        var convertData = function (data) {
            var res = [];
            for (var i = 0; i < data.length; i++) {
                var geoCoord = geoCoordMap[data[i].name];
                if (geoCoord) {
                    res.push({
                        name: data[i].name,
                        value: geoCoord.concat(data[i].value)
                    });
                }
            }
            return res;
        };
        var option = {
            backgroundColor: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#0f378f' // 0% 处的颜色
                }, {
                    offset: 1, color: '#00091a' // 100% 处的颜色
                }],
                globalCoord: false // 缺省为 false
            },
            title: {
                top: 20,
                text: '“会员活跃度”',
                subtext: '',
                x: 'center',
                textStyle: {
                    color: '#ccc'
                }
            },

            tooltip: {
                trigger: 'item',
                formatter: function (params) {
                    if (typeof (params.value)[2] == "undefined") {
                        return params.name + ' : ' + params.value;
                    } else {
                        return params.name + ' : ' + params.value[2];
                    }
                }
            },
            /*   legend: {
                   orient: 'vertical',
                   y: 'bottom',
                   x: 'right',
                    data:['pm2.5'],
                   textStyle: {
                       color: '#fff'
                   }
               },*/
            legend: {
                orient: 'vertical',
                y: 'bottom',
                x: 'right',
                data: ['pm2.5'],
                textStyle: {
                    color: '#fff'
                }
            },
            visualMap: {
                show: false,
                min: 0,
                max: 500,
                left: 'left',
                top: 'bottom',
                text: ['高', '低'], // 文本，默认为数值文本
                calculable: true,
                seriesIndex: [1],
                inRange: {}
            },
            geo: {
                map: nameMap,
                show: true,
                roam: true,
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: false,
                    }
                },
                itemStyle: {
                    normal: {
                        areaColor: '#3a7fd5',
                        borderColor: '#0a53e9',//线
                        shadowColor: '#092f8f',//外发光
                        shadowBlur: 20
                    },
                    emphasis: {
                        areaColor: '#0a2dae',//悬浮区背景
                    }
                }
            },
            series: [
                {
                    symbolSize: 5,
                    label: {
                        normal: {
                            formatter: '{b}',
                            position: 'right',
                            show: true
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: '#fff'
                        }
                    },
                    name: 'light',
                    type: 'scatter',
                    coordinateSystem: 'geo',
                    data: convertData(customerBatteryCityData),
                },
                {
                    type: 'map',
                    map: nameMap,
                    geoIndex: 0,
                    aspectScale: 0.75, //长宽比
                    showLegendSymbol: false, // 存在legend时显示
                    label: {
                        normal: {
                            show: false
                        },
                        emphasis: {
                            show: false,
                            textStyle: {
                                color: '#fff'
                            }
                        }
                    },
                    roam: true,
                    itemStyle: {
                        normal: {
                            areaColor: '#031525',
                            borderColor: '#FFFFFF',
                        },
                        emphasis: {
                            areaColor: '#2B91B7'
                        }
                    },
                    animation: false,
                    data: data
                },
                {
                    name: 'Top 5',
                    type: 'scatter',
                    coordinateSystem: 'geo',
                    symbol: 'pin',
                    symbolSize: [50, 50],
                    label: {
                        normal: {
                            show: true,
                            textStyle: {
                                color: '#fff',
                                fontSize: 9,
                            },
                            formatter(value) {
                                return value.data.value[2]
                            }
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: '#D8BC37', //标志颜色
                        }
                    },
                    data: convertData(customerBatteryCityData),
                    showEffectOn: 'render',
                    rippleEffect: {
                        brushType: 'stroke'
                    },
                    hoverAnimation: true,
                    zlevel: 1
                },

            ]
        };
        myChart.clear()
        myChart.resize()
        myChart.setOption(option)
    }

    if (window.location.origin == 'file://') {
        setTimeout(() => {
            getEcharts(gdMap)
        }, 100)
    } else {
        getData()
    }
</script>
</html>
<style>
    html, body {
        padding: 0;
        margin: 0;
    }

    #home {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .echarts {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .info {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;
        color: red;
    }

    .links {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;

    }

    .links a {
        text-decoration: none;
        color: #fff;
    }
</style>
